open Plplot
open Pldraw

external make_pixmap : Gtk.widget Gtk.obj -> Gdk.pixmap = "make_pixmap"

external ev_plotwindow_configure : Gtk.widget Gtk.obj -> 
    Gdk.pixmap -> Gdk.pixmap =
    "ev_plotwindow_configure"
external ev_plotwindow_expose : Gtk.widget Gtk.obj -> 
    Gdk.pixmap -> GdkEvent.Expose.t -> bool = "ev_plotwindow_expose"
external ev_plotwindow_expose_all : Gtk.widget Gtk.obj -> 
    Gdk.pixmap -> bool = "ev_plotwindow_expose_all"

let draw_on_event ?(size=(1600,1200))
  ?(plot_init: unit -> unit = (fun () -> init (1,1))) 
  area f =
(*    let w, h = (fst size)+1, (snd size)+1 in*)
      print_endline "doe:beg";
    let w, h = (fst size), (snd size) in
    let size_str = Printf.sprintf "%ix%i" w h in
    plparseopts [|"open";"-geometry";size_str|] [PL_PARSE_FULL];
    Plplot.plsdev "xcairo";
    Plplot.plsetopt "drvopt" "external_drawable";
    Plplot.plinit ();
    plot_init ();
(*    let b=ev_plotwindow_expose area#as_widget in*)
    f ();
      print_endline "doe:end";
(*    b*)
    true
